package top.italks.esp32app.bluetooth;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import top.italks.esp32app.R;

public class BluetoothListActivity extends AppCompatActivity {
    private BluetoothAdapter bluetoothAdapter;
    private BluetoothListAdapter listAdapter;
    private RecyclerView recyclerView;
    private List<BluetoothDevice> deviceList;
    private String TAG="BluetoothListActivity";
    private String deviceMac;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bluetooth_list);
        initRecyclerview();
        initBT();
    }
    private void initRecyclerview(){
        listAdapter=new BluetoothListAdapter();
        recyclerView=findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(listAdapter);
        deviceList=new ArrayList<>();
        listAdapter.setDeviceList(deviceList);
        listAdapter.setOnClickListener(new BluetoothListAdapter.OnClickListener() {
            @Override
            public void OnItemClick(int position, BluetoothDevice device) {
                Log.d(TAG,"address="+device.getAddress());
                bluetoothAdapter.cancelDiscovery();
                deviceMac=device.getAddress();
                Intent intent=new Intent();
                intent.putExtra("deviceMac",deviceMac);
                setResult(RESULT_OK,intent);
                finish();
            }
        });
    }

    protected void initBT(){
        bluetoothAdapter= BluetoothAdapter.getDefaultAdapter();
        if (!bluetoothAdapter.isEnabled()) {
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, 10);
        }
        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        IntentFilter filter2 = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        registerReceiver(receiver, filter);
        registerReceiver(receiver, filter2);
        bluetoothAdapter.startDiscovery();
    }
    private final BroadcastReceiver receiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                // Discovery has found a device. Get the BluetoothDevice
                // object and its info from the Intent.
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                String deviceName = device.getName();
                String deviceHardwareAddress = device.getAddress(); // MAC address
                Log.d("BT",String.format("DeviceName=%s,MacAddress=%s",deviceName,deviceHardwareAddress));
                if (deviceName!=null){
                    if (deviceName.contains("ESP32")) {
                        deviceList.add(device);
                    }
                }
                listAdapter.notifyDataSetChanged();
            }else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
                Log.d("BT","搜索完成");

            }
        }
    };
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(receiver);
    }


}